const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

function resolve(dir = '') {
  return path.join(__dirname, './src', dir)
}

module.exports = {
  // webpack-dev-server 相关配置
  devServer: {
    open: false,
    host: '0.0.0.0',
    port: 8080,
    https: false,
    hotOnly: false,
    disableHostCheck: true,
    // 设置代理
    proxy: {
      '/api': {
        target: 'http://yapi.demo.qunar.com/mock/9343',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '^/api': '/api'
        }
      },
    }
    // before: app => {}
  },
  publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  // 生产环境关eslint
  lintOnSave: process.env.NODE_ENV !== 'production',
  chainWebpack: config => {
    // config.resolve.alias.set('@$', resolve('src'))
    // // webpackPrefetch: true 时生效,在闲置时获取资源
    // // webpackPreload: true 时生效,正常获取，及早发现
    // config.plugins.delete('prefetch');
    // config.plugins.delete('preload');
  },
  configureWebpack: {
    plugins: [
      // new LodashModuleReplacementPlugin()
      // // 去除console
      // new UglifyJsPlugin({
      //   uglifyOptions: {
      //     compress: {
      //       warnings: false,
      //       drop_debugger: true,
      //       drop_console: true,
      //     },
      //   },
      //   sourceMap: true,
      //   parallel: true,
      // }),
    ],
    resolve: {
      alias: {
        core: resolve('core')
      }
    },
    optimization: {
      runtimeChunk: {
        name: entrypoint => `runtime~${entrypoint.name}`
      },
      splitChunks: {
        minChunks: 2,
        minSize: 10000,
        maxAsyncRequests: 30,
        maxInitialRequests: 20,
        name: false
      }
    }
  },
  // sass-loader配置
  css: {
    loaderOptions: {
      sass: {
        // 这里的选项会传递给 sass-loader
        data: '@import "@/scss/settings.scss";'
      }
    },
    // 生产环境开启sourceMap
    sourceMap: process.env.NODE_ENV === 'production'
  }
}